﻿using System;
using System.Collections.Generic;
using D2D2.Networking;
using D2D2.SharedInterfaces.Networking;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace D2D2.Core.SpriteManager
{
    public class BattlefieldClient
    {
        private const int OFFSET_Y = 100;
        private const int ZONE_HEIGH = 222;
        private const int BOSS_WIDTH = 180;
        private const int MOSTER2_WIDTH = 150;
        private const int MONSTER_WIDTH = 100;
        private const int PADDING = 10;
        private const int LIVE_ZONE_HEIGHT = 20;

        private int firstBorder = OFFSET_Y + ZONE_HEIGH;
        private int secondBorder = OFFSET_Y + ZONE_HEIGH*2;

        private MonsterManagerClient _monsterManager = new MonsterManagerClient();
        private ChestManagerClient _chestManager = new ChestManagerClient();
        private DropManagerClient _dropManager = new DropManagerClient();

        private readonly Random _random = new Random();
        private ClientManager _clientManager;

        public BattlefieldClient(ClientManager clientManager)
        {
            _clientManager = clientManager;

            //Queue<string> q = new Queue<string>();
        }


        public void Generate()
        {
            _clientManager.SendMessage(new SignalMessage {SignalType = SignalType.GenerateBattlefield});
        }

        public void UpdateMonster(NetIncomingMessage message)
        {
            MonsterObject monsterObject = new MonsterObject(message);

            if (!_monsterManager.Exist(monsterObject))
                AddMonster(monsterObject);
            else
                _monsterManager.Update(monsterObject);
        }

        public void UpdateDrop(NetIncomingMessage message)
        {
            Drop drop = new Drop(message) {Texture = BattleSprite.DropTexture};
            _dropManager.Update(drop);
        }

        public void UpdateChest(NetIncomingMessage message)
        {
            ChestObject chestObject = new ChestObject(message);
            if (!_chestManager.Exist(chestObject))
                AddChest(chestObject);
            else
                _chestManager.Update(chestObject);
        }

        private void AddMonster(MonsterObject monsterObject)
        {
            SpriteType monsterView = (SpriteType) monsterObject.MonsterView;

            Rectangle rectangle = new Rectangle((int) monsterObject.Location.X, (int) monsterObject.Location.Y,
                                                monsterObject.GetWidth(), monsterObject.GetHeight());

            BattleSprite monster = _monsterManager.Add(rectangle, monsterView, monsterObject);
            monster.LiveRating = monsterObject.LiveRegen;
        }

        public void AddChest(ChestObject chestObject)
        {
            Rectangle rectangle = new Rectangle((int)chestObject.Location.X, (int)chestObject.Location.Y,
                                                chestObject.GetWidth(), chestObject.GetHeight());

            Chest chest = _chestManager.Add(rectangle, chestObject);
        }

        public void MouseClickHandle(int x, int y)
        {
            _monsterManager.MouseClickHandle(x, y);
            _clientManager.SendMessage(new MouseMessage {X = x, Y = y, PlayerId = Global.PlayerManager.Player.Id});
        }

        public void Update(GameTime gameTime)
        {
            _monsterManager.Update(gameTime);
            _chestManager.Update(gameTime);
            _dropManager.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            _monsterManager.Draw(spriteBatch);
            _chestManager.Draw(spriteBatch);
            _dropManager.Draw(spriteBatch);
        }

        private SpriteType GetMonsterType()
        {
            return (SpriteType) _random.Next(1, 3);
        }

        /*
        private void AddMonster2(int x)
        {
            BattleSprite monster =
                _monsterManager.Add(new Rectangle(x, firstBorder + PADDING + LIVE_ZONE_HEIGHT, 150, MOSTER2_WIDTH),
                                    GetMonsterType(), 100);
            monster.LiveRating = 2;
        }

        private void AddMonster(int x)
        {
            BattleSprite monster =
                _monsterManager.Add(new Rectangle(x, secondBorder + PADDING + LIVE_ZONE_HEIGHT, 100, MONSTER_WIDTH),
                                    GetMonsterType(), 50);
        }
        */
    }
}
